<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Users (Users list (Innovators))
 * @description : Show list of users on innoTweets
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Users extends AUTH_Controller {

  /**
   * Show users list
   * 
   */
  public function index($limit = NULL) {

	$offset = (int) ceil($limit / 50) * 50;

	// Getting the users data according to the limit
	$get_all_users = $this->user_model->get_all_users(50, $offset, array(1, 2));
	$users_data = $get_all_users['result'];

	if (!empty($users_data)) {

	  // Getting all users data & count
	  $get_all_users = $this->user_model->get_all_users();
	  $total_users_count = $get_all_users['count'];

	  // Preparing the view data
	  $view_data = array();
	  $view_data['menu_active'] = 'users';
	  $view_data['users_data'] = $users_data;
	  $view_data['offset'] = $offset;
	  $view_data['total_users_count'] = $total_users_count;

	  // Loading the view and passing the view data
	  $this->load_view('users', $view_data);
	  return;
	}

	redirect('users');
  }

  public function users_list() {
	$user_name = $this->input->post('user_name', TRUE);
	$user_name = @substr($user_name, 1);
	$get_user_search = $this->user_model->get_user_search($user_name);
	if (!empty($user_name) && $get_user_search['status'] == 1 && !empty($get_user_search['result'])) {
	  $view_data = array();
	  $view_data['users_data'] = $get_user_search['result'];
	  $this->load->view('users_list', $view_data);
	}
	return;
  }

  /**
   * Resign a user (Change user_resigned to 1)
   * 
   */
  public function disable($user_name = NULL) {
	if (($this->is_admin) && (!check_is_admin($user_name))) {
	  $get_user_by_username = $this->user_model->get_user_by_username($user_name);
	  if ($get_user_by_username['status'] == 1 && !empty($get_user_by_username['result'])) {
		$user_id = $get_user_by_username['result']['user_id'];
		$user_data = array();
		$user_data['user_resigned'] = 1;
		$user_data['user_likes_level'] = 0;
		$this->user_model->update_profile($user_id, $user_data);
	  }
	}
	redirect('users');
  }

  /**
   * Enable a user (Change user_resigned to 0)
   * 
   */
  public function enable($user_name = NULL) {
	if (($this->is_admin) && (!check_is_admin($user_name))) {
	  $get_user_by_username = $this->user_model->get_user_by_username($user_name);
	  if ($get_user_by_username['status'] == 1 && !empty($get_user_by_username['result'])) {
		$user_id = $get_user_by_username['result']['user_id'];
		$user_data = array();
		$user_data['user_resigned'] = 0;
		$user_data['user_likes_level'] = 0;
		$this->user_model->update_profile($user_id, $user_data);
	  }
	}
	redirect('users');
  }

  /**
   * Update user activity points
   */
  public function update_all_points() {
	$this->load->library('user_reputation');
	$get_all_users = $this->user_model->get_all_users();
	if (!empty($get_all_users['result'])) {
	  foreach ($get_all_users['result'] as $user) {
		if ($user['user_resigned'] == 0) {
		  $user_update_data['user_likes_level'] = (int) $this->user_reputation->init($user['user_id']);
		  $this->user_model->update_profile($user['user_id'], $user_update_data);
		}
	  }
	}
	redirect('users');
  }

  /**
   * Update current user points
   */
  public function update_my_points() {
	$user_update_data['user_likes_level'] = (int) $this->user_reputation->init($this->current_user['user_id']);
	$this->user_model->update_profile($this->current_user['user_id'], $user_update_data);
  }

}

?>